/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AnimalSelector.java
 *
 * Created on Apr 9, 2009, 12:21:05 PM
 */

package doghost.selectors;

import javax.swing.RowFilter;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableRowSorter;
import org.jdesktop.application.Action;
import doghost.controllers.AnimalController;
import doghost.entities.Animal;

/**
 *
 * @author duo
 */
public class AnimalSelector extends javax.swing.JDialog {

    protected AnimalController animalController;
    protected Animal selectedAnimal;

    /** Creates new form AnimalSelector */
    public AnimalSelector(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        animalController = new AnimalController();
        animalTable.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {


            @Override
                    public void valueChanged(ListSelectionEvent e) {
                        firePropertyChange("animalRecordSelected",
                                !isAnimalRecordSelected(), isAnimalRecordSelected());
                    }
                });
    }

    public Animal getSelectedAnimal() {
        return selectedAnimal;
    }

    public void setSelectedAnimal(Animal selectedAnimal) {
        this.selectedAnimal = selectedAnimal;
    }

    public boolean isAnimalRecordSelected() {
        return animalTable.getSelectedRow() != -1;
    }

    @Action
    public void newAnimal() {
        animalController.newAnimal(doghostPUEntityManager, animalList);
    }

    @Action(enabledProperty = "animalRecordSelected")
    public void editAnimal() {
        int index = animalTable.getSelectedRow();
        animalController.setAnimal(animalList.get(animalTable.
                convertRowIndexToModel(index)));
        animalController.editAnimal(doghostPUEntityManager, animalList,
                animalTable.convertRowIndexToModel(index));
    }

    @Action(enabledProperty = "animalRecordSelected")
    public void deleteAnimal() {
        int index = animalTable.getSelectedRow();
        animalController.setAnimal(animalList.get(animalTable.
            convertRowIndexToModel(index)));
        animalController.deleteAnimal(doghostPUEntityManager, animalList,
            animalTable.convertRowIndexToModel(index));
    }

    @Action
    public void cancelAnimalSelector() {
        setSelectedAnimal(null);
        setVisible(false);
    }

    @Action(enabledProperty = "animalRecordSelected")
    public void selectAnimal() {
        int index = animalTable.getSelectedRow();
        setSelectedAnimal(animalList.get(animalTable.
                convertRowIndexToModel(index)));
        setVisible(false);
    }

    @Action
    public void filterAnimal() {
        TableRowSorter animalSorter = (TableRowSorter) animalTable.getRowSorter();
        String text = animalFilterTextField.getText();
        animalFilterTextField.setText("");
        if(text.length()==0) {
            animalSorter.setRowFilter(null);
        } else {
            animalSorter.setRowFilter(RowFilter.regexFilter("(?u)(?i)" + text));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        doghostPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("doghostPU").createEntityManager();
        animalQuery = java.beans.Beans.isDesignTime() ? null : doghostPUEntityManager.createQuery("SELECT a FROM Animal a");
        animalList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(animalQuery.getResultList());
        animalFilterTextField = new javax.swing.JTextField();
        animalFilterButton = new javax.swing.JButton();
        newButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        animalSelectionPanel = new org.jdesktop.swingx.JXPanel();
        animalScrollPane = new javax.swing.JScrollPane();
        animalTable = new javax.swing.JTable();
        selectButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getResourceMap(AnimalSelector.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        animalFilterTextField.setName("animalFilterTextField"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getActionMap(AnimalSelector.class, this);
        animalFilterButton.setAction(actionMap.get("filterAnimal")); // NOI18N
        animalFilterButton.setName("animalFilterButton"); // NOI18N

        newButton.setAction(actionMap.get("newAnimal")); // NOI18N
        newButton.setName("newButton"); // NOI18N

        editButton.setAction(actionMap.get("editAnimal")); // NOI18N
        editButton.setName("editButton"); // NOI18N

        deleteButton.setAction(actionMap.get("deleteAnimal")); // NOI18N
        deleteButton.setName("deleteButton"); // NOI18N

        cancelButton.setAction(actionMap.get("cancelAnimalSelector")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N

        animalSelectionPanel.setName("animalSelectionPanel"); // NOI18N

        animalScrollPane.setName("animalScrollPane"); // NOI18N

        animalTable.setAutoCreateRowSorter(true);
        animalTable.setName("animalTable"); // NOI18N

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, animalList, animalTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nome}"));
        columnBinding.setColumnName("Nome");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${cliente}"));
        columnBinding.setColumnName("Cliente");
        columnBinding.setColumnClass(doghost.entities.Cliente.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        animalScrollPane.setViewportView(animalTable);

        javax.swing.GroupLayout animalSelectionPanelLayout = new javax.swing.GroupLayout(animalSelectionPanel);
        animalSelectionPanel.setLayout(animalSelectionPanelLayout);
        animalSelectionPanelLayout.setHorizontalGroup(
            animalSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 529, Short.MAX_VALUE)
            .addGroup(animalSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(animalSelectionPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(animalScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 505, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        animalSelectionPanelLayout.setVerticalGroup(
            animalSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 239, Short.MAX_VALUE)
            .addGroup(animalSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(animalSelectionPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(animalScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                    .addContainerGap()))
        );

        selectButton.setAction(actionMap.get("selectAnimal")); // NOI18N
        selectButton.setText(resourceMap.getString("selectButton.text")); // NOI18N
        selectButton.setName("selectButton"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(animalFilterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(animalFilterButton))
                    .addComponent(animalSelectionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newButton)
                        .addGap(18, 18, 18)
                        .addComponent(editButton)
                        .addGap(18, 18, 18)
                        .addComponent(deleteButton)
                        .addGap(18, 18, 18)
                        .addComponent(cancelButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 145, Short.MAX_VALUE)
                        .addComponent(selectButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(animalFilterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(animalFilterButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(animalSelectionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(newButton)
                    .addComponent(editButton)
                    .addComponent(deleteButton)
                    .addComponent(cancelButton)
                    .addComponent(selectButton))
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AnimalSelector dialog = new AnimalSelector(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton animalFilterButton;
    private javax.swing.JTextField animalFilterTextField;
    private java.util.List<doghost.entities.Animal> animalList;
    private javax.persistence.Query animalQuery;
    private javax.swing.JScrollPane animalScrollPane;
    private org.jdesktop.swingx.JXPanel animalSelectionPanel;
    private javax.swing.JTable animalTable;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton deleteButton;
    private javax.persistence.EntityManager doghostPUEntityManager;
    private javax.swing.JButton editButton;
    private javax.swing.JButton newButton;
    private javax.swing.JButton selectButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
